package com.digitalbonestudios.slugpug.tv;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import com.digitalbonestudios.framework.Game;
import com.digitalbonestudios.framework.Input.KeyEvent;
import com.digitalbonestudios.framework.Input.TouchEvent;
import com.digitalbonestudios.framework.gl.Camera2D;
import com.digitalbonestudios.framework.gl.SpriteBatcher;
import com.digitalbonestudios.framework.impl.GLScreen;
import com.digitalbonestudios.framework.math.OverlapTester;
import com.digitalbonestudios.framework.math.Vector2;

public class MenuScreen extends GLScreen {
	enum MenuSelection {
		Play,
		Quit
	}
	
	Camera2D guiCam;
	Vector2 touchPoint;
	SpriteBatcher batcher;
	World world;
    WorldRenderer renderer;
    MenuSelection _menuSelection = MenuSelection.Play;
    
    GenericObject playButton;
    GenericObject quitButton;
    MenuSelection lastMenu;

	public MenuScreen(Game game) {
		super(game);
		guiCam = new Camera2D(glGraphics, 768, 448);
		touchPoint = new Vector2();
		batcher = new SpriteBatcher(glGraphics, 1000);
		world = new World();
        renderer = new WorldRenderer(glGame.getContext(), glGraphics, batcher, world);
        playButton = new GenericObject(488, 304, 192, 64);
        quitButton = new GenericObject(488, 208, 192, 64);
	}

	@Override
	public void update(float deltaTime) {
		//Handle Touch Input
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
	    int len = touchEvents.size();
	    for(int i = 0; i < len; i++) {
	    	TouchEvent event = touchEvents.get(i);
	        //if(event.type != TouchEvent.TOUCH_UP)
	        //    continue;
	        
	        touchPoint.set(event.x, event.y);
	        guiCam.touchToWorld(touchPoint);
	        
	        if (event.type != TouchEvent.TOUCH_UP) {
	        	lastMenu = _menuSelection;
	        	
	        	//Play
		        if(OverlapTester.pointInRectangle(playButton.bounds, touchPoint)) {
		        	_menuSelection = MenuSelection.Play;
		        }
		        
		        //Quit
		        if(OverlapTester.pointInRectangle(quitButton.bounds, touchPoint)) {
		        	_menuSelection = MenuSelection.Quit;
		        }
	        }
	        
	        if (event.type == TouchEvent.TOUCH_UP) {
		        //Play
		        if(OverlapTester.pointInRectangle(playButton.bounds, touchPoint)) {
		        	Log.v("SlugPug", "Play Game");
		        	game.setScreen(new SlugPug(game));
		        }
		        
		        //Quit
		        if(OverlapTester.pointInRectangle(quitButton.bounds, touchPoint)) {
		        	Log.v("SlugPug", "Quit Game");
		        	glGame.finish();
		        }
	        }
	        
	        //_menuSelection = lastMenu;
	    }
		
		//Handle D-Pad Input
		game.getInput().getTouchEvents();
        List<KeyEvent> keyEvents = game.getInput().getKeyEvents();
        processKeyEvents(keyEvents);
	}
	
	private void processKeyEvents(List<KeyEvent> keyEvents) {
		int len = keyEvents.size();
    	
    	for (int i = 0; i < len; i++) {
    		KeyEvent event = keyEvents.get(i);
    		switch (event.keyCode) {
    		case KeyEvent.KEYCODE_DPAD_UP:
    			_menuSelection = MenuSelection.Play;
    			break;
		    case KeyEvent.KEYCODE_DPAD_DOWN:
		    	_menuSelection = MenuSelection.Quit;
		    	break;
		    case KeyEvent.KEYCODE_DPAD_CENTER:
		    	if (_menuSelection == MenuSelection.Quit)
		    		glGame.finish();
		    	else
		    		game.setScreen(new SlugPug(game));
		    	break;
    		}
    		Log.v("SlugPug", "KeyCode: " + event.keyCode );
    	}
	}
	
	@Override
	public void present(float deltaTime) {
		GL10 gl = glGraphics.getGL();
	    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	    gl.glEnable(GL10.GL_TEXTURE_2D);
	    gl.glEnable(GL10.GL_BLEND);
        //gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
	    
	    //renderer.render();
       
	    
	    batcher.beginBatch(Assets.pugItems);
			//batcher.drawSprite(400, 240, 800, 480, Assets.background);
	    	batcher.drawSprite(384, 224, 768, 448, Assets.background);
	    	batcher.drawSprite(225, 240, 256, 256, Assets.logo);
	    	
	    	batcher.drawSprite(playButton.position.x, playButton.position.y, playButton.width, playButton.height, Assets.playButton);
	    	batcher.drawSprite(quitButton.position.x, quitButton.position.y, quitButton.width, quitButton.height, Assets.quitButton);
	    	
			drawMenuSelector();
		batcher.endBatch();
		
		guiCam.setViewportAndMatrices();
		gl.glDisable(GL10.GL_BLEND);
		
	}
	
	private void drawMenuSelector() {
		if (_menuSelection == MenuSelection.Play) {
			batcher.drawSprite(618, 304, 64, 64, Assets.pawPrint);
		} else {
			batcher.drawSprite(618, 208, 64, 64, Assets.pawPrint);
		}
	}

	@Override
	public void pause() {
		
		
	}

	@Override
	public void resume() {
		
		
	}

	@Override
	public void dispose() {
		
		
	}

}
